using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using System.Linq;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using SukiUI;
using SysLog;
using UI.ViewModels;
using UI.Views;

namespace UI;

public partial class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        //配置数据库日志
        // ILog.ConfigureSerilogToMysql();
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // Avoid duplicate validations from both Avalonia and the CommunityToolkit. 
            // More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
            DisableAvaloniaDataAnnotationValidation();
            // navigateToMainPage(desktop);
            SukiTheme.GetInstance().ChangeBaseTheme(ThemeVariant.Light);
            navigateToLoginPage(desktop);
        }

        base.OnFrameworkInitializationCompleted();
    }

    public void navigateToMainPage(IClassicDesktopStyleApplicationLifetime desktop)
    {
        var mainWindow = new MainWindow
        {
            DataContext = new MainWindowViewModel(),
        };
        desktop.MainWindow = mainWindow;
        desktop.MainWindow.Show();
    }

    public void navigateToLoginPage(IClassicDesktopStyleApplicationLifetime desktop)
    {
        var loginViewModel = new LoginViewModel();
        var loginWindow = new LoginWindow();
        loginWindow.DataContext = loginViewModel;
        desktop.MainWindow = loginWindow;
        desktop.MainWindow.Show();
    }

    private void DisableAvaloniaDataAnnotationValidation()
    {
        // Get an array of plugins to remove
        var dataValidationPluginsToRemove =
            BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();

        // remove each entry found
        foreach (var plugin in dataValidationPluginsToRemove)
        {
            BindingPlugins.DataValidators.Remove(plugin);
        }
    }
}